package com.android.kotlincoroutinemvvmdemo.view
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.graphics.Rect
import android.util.AttributeSet
import android.util.Log
import com.android.kotlincoroutinemvvmdemo.R

class GoogleLogoView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : SurfaceViewTemplate(context, attributeSet) {

    private val mPaint0 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            style = Paint.Style.FILL
            //color = context.resources.getColor(R.color.color_CF0FEF)
            color = context.resources.getColor(R.color.color_FFD740)
        }
    }

    private val mPaint1 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            style = Paint.Style.FILL
            color = context.resources.getColor(R.color.color_00796B)
        }
    }

    private val mPaint2 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            style = Paint.Style.FILL
            color = context.resources.getColor(R.color.color_FF0000)
        }
    }

    private val mPaint3 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            style = Paint.Style.FILL
            color = context.resources.getColor(R.color.color_0C46E6)
        }
    }

    private val mPaint4 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            style = Paint.Style.STROKE
            color = context.resources.getColor(R.color.color_FFFFFF)
        }
    }

    private val mPath by lazy {
        Path()
    }

    init {
        mHolder.addCallback(this)
    }

    override fun Canvas.onDrawContent() {
        val cx = mWidth.toFloat() / 2
        val cy = mHeight.toFloat() / 2

        translate(cx, cy)
        rotate(6F)

        drawColor(context.resources.getColor(R.color.color_F3F3F3))
        val mStrokeWidth = (cx * 1 / 15).coerceAtLeast(minStrokeWidth)
        mPaint4.strokeWidth = mStrokeWidth

        mPath.apply {
            reset()

            moveTo(0F, -cy / 3)
            lineTo(
                cx * Math.cos(Math.toRadians(-30.0)).toFloat(),
                cx * Math.sin(Math.toRadians(-30.0)).toFloat()
            )
            lineTo(
                cx / 3 * Math.cos(Math.toRadians(-30.0)).toFloat(),
                cx / 3 * Math.sin(Math.toRadians(-30.0)).toFloat()
            )

            addArc(
                -cx,
                -cy,
                cx,
                cy,
                -30F,
                30F,
            )

            lineTo(0F, 0F)

            arcTo(
                -cx,
                -cy,
                cx,
                cy,
                0F,
                90F,
                true
            )
            quadTo(cx / 3, cy / 3, cx / 3, 0F)

            close()
        }
        drawPath(mPath, mPaint0)
        //==========================================================================================

        mPath.apply {
            reset()
            moveTo(0F, cy)
            lineTo(0F, 0F)
            lineTo(cx / 3, 0F)
            quadTo(cx / 3, cy / 3, 0F, cy)

            arcTo(
                -cx,
                -cy,
                cx,
                cy,
                90F,
                120F,
                true
            )
            quadTo(-cx / 3, cy / 3, 0F, cy / 3)
            close()
        }
        drawPath(mPath, mPaint1)
        //==========================================================================================

        mPath.apply {
            reset()
            arcTo(
                -cx,
                -cy,
                cx,
                cy,
                210F,
                120F,
                true
            )

            moveTo(cx * Math.cos(Math.toRadians(210.0)).toFloat(),
                cx * Math.sin(Math.toRadians(210.0)).toFloat())
            quadTo(-cx / 3, cy / 3, 0F, cy / 3)
            lineTo(0F, 0F)

            moveTo(0F, -cy / 3)
            lineTo(
                cx * Math.cos(Math.toRadians(210.0)).toFloat(),
                cx * Math.sin(Math.toRadians(210.0)).toFloat()
            )
            lineTo(
                cx / 3 * Math.cos(Math.toRadians(210.0)).toFloat(),
                cx / 3 * Math.sin(Math.toRadians(210.0)).toFloat()
            )

            moveTo(0F, -cy / 3)
            lineTo(
                cx * Math.cos(Math.toRadians(210.0)).toFloat(),
                cx * Math.sin(Math.toRadians(210.0)).toFloat()
            )
            lineTo(
                cx * Math.cos(Math.toRadians(-30.0)).toFloat(),
                cx * Math.sin(Math.toRadians(-30.0)).toFloat()
            )
            close()
        }
        drawPath(mPath, mPaint2)
        //==========================================================================================

        drawCircle(
            0F,
            0F,
            cx / 3,
            mPaint3
        )

        drawCircle(
            0F,
            0F,
            cx / 3,
            mPaint4
        )
    }
}